package task

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	. "iot-base/auth/api/crontab"
	"iot-base/auth/api/logic"
	types2 "iot-base/auth/types"
	"time"
)

func Add(c *gin.Context) {
	resp := types2.NewResult(c)
	var req types2.ReqAddTask
	err := c.ShouldBind(&req)
	if err != nil {
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	// 启用 禁用
	if req.Switch == "" {
		req.Switch = "enable"
	}
	rules := make([]types2.CaliParams, 0, 1)
	rules = append(rules, types2.CaliParams{
		GasType:   types2.IsZero(req.TaskType),
		Number:    float64(req.Number),
		Ext:       req.Ext,
		Unit:      types2.GetUnit(req.ModelId),
		ValueTime: []int{},
		Flow:      req.Flow,
	})
	b, _ := json.Marshal(rules)
	task, err := logic.NewTaskLogic().AddTask(req, b)
	if err != nil {
		resp.ErrorCode(types2.Fail, "添加任务失败.")
		return
	}
	if req.Switch == "enable" {
		// 添加定时任务.
		execTime, _ := time.ParseInLocation("15:04:05", task.ExecTime, time.Local)
		_ = Cron.AddTask(task.CodeId, req.TimeType, execTime, req.Day)
	}
	resp.Success(gin.H{})
}

func Del(c *gin.Context) {
	resp := types2.NewResult(c)
	var req types2.ReqDelTask
	err := c.ShouldBind(&req)
	if err != nil {
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	codeId, err := logic.NewTaskLogic().DelTaskById(req.Id)
	if err != nil {
		resp.ErrorCode(types2.Fail, "")
		return
	}
	// 移除定时任务.
	Cron.RemoveTask(codeId)
	resp.Success(gin.H{})
}

func Modify(c *gin.Context) {
	resp := types2.NewResult(c)
	var req types2.ReqModifyTask
	err := c.ShouldBind(&req)
	if err != nil {
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	rules := make([]types2.CaliParams, 0, 1)
	rules = append(rules, types2.CaliParams{
		GasType:   types2.IsZero(req.TaskType),
		Number:    float64(req.Number),
		Ext:       req.Ext,
		Unit:      types2.GetUnit(req.ModelId),
		ValueTime: []int{},
		Flow:      req.Flow,
	})
	b, _ := json.Marshal(rules)
	task, err := logic.NewTaskLogic().ModifyTaskById(req, b)
	if err != nil {
		resp.ErrorCode(types2.Fail, "添加任务失败.")
		return
	}
	// 添加定时任务.
	Cron.RemoveTask(task.CodeId)
	if task.TaskType == types2.ZeroDriftAndSpanDrift {
		Cron.RemoveSubTask(task.RunTaskLogId)
	}
	if req.Switch != "disable" {
		// 禁用之后不用，更新任务
		execTime, _ := time.ParseInLocation("15:04:05", task.ExecTime, time.Local)
		//execTime, _ := utils.StringToTime(task.ExecTime)
		_ = Cron.AddTask(task.CodeId, req.TimeType, execTime, req.Day)
	}
	resp.Success(gin.H{})
	return
}

func List(c *gin.Context) {
	resp := types2.NewResult(c)
	var req types2.ReqTaskList
	err := c.ShouldBind(&req)
	if err != nil {
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	lists, err := logic.NewTaskLogic().FindTaskByTimeType(req.TimeType, req.Page, req.Size)
	if err != nil {
		resp.ErrorCode(types2.DBError, "")
		return
	}
	resp.Success(gin.H{"list": lists})
	return
}

func FindTaskLog(c *gin.Context) {
	resp := types2.NewResult(c)
	var req types2.ReqFindTaskLog
	err := c.ShouldBind(&req)
	if err != nil {
		fmt.Println(err)
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	result, err := logic.NewTaskLogic().FindLog(req.StationId, req.Page, req.Size, req.PlanType)
	if err != nil {
		resp.ErrorCode(types2.DBError, "")
		return
	}
	resp.Success(result)
	return
}

func LogDetails(c *gin.Context) {
	resp := types2.NewResult(c)
	var req types2.ReqLogDetails
	err := c.ShouldBind(&req)
	if err != nil {
		fmt.Println(err)
		resp.ErrorCode(types2.ParamsError, "")
		return
	}
	result, err := logic.NewTaskLogic().FindLogDetail(req.Id)
	if err != nil {
		resp.ErrorCode(types2.DBError, "")
		return
	}
	resp.Success(result)
	return
}
